package com.android.test;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * @author impaler This is the main surface that handles the ontouch events and
 *         draws the image to the screen.
 */

public class MainGamePanel extends SurfaceView implements
		SurfaceHolder.Callback { // SurfaceHolder.callback lets us know about
									// surface changes

	private static final String TAG = MainGamePanel.class.getSimpleName();

	private MainThread thread;
	private Droid droid, droid2;

	public MainGamePanel(Context context) {
		super(context);
		// adding the callback (this) to the surface holder to intercept events
		getHolder().addCallback(this);

		// create droid and load bitmap
		droid = new Droid(BitmapFactory.decodeResource(getResources(),
				R.drawable.droid_1), 50, 50);
		droid2 = new Droid(BitmapFactory.decodeResource(getResources(),
				R.drawable.droid_1), 350, 350);

		// create the game loop thread
		thread = new MainThread(getHolder(), this);

		// make the GamePanel focusable so it can handle events
		setFocusable(true);
	}

	/**
	 * This is the game update method. It iterates through all the objects and
	 * calls their update method if they have one or calls specific engine's
	 * update method.
	 */

	public void update() {
		// check collision with right wall if heading right
		if (droid.getSpeed().getxDirection() == Speed.DIRECTION_RIGHT
				&& droid.getX() + droid.getBitmap().getWidth() / 2 >= getWidth()) {
			droid.getSpeed().toggleXDirection();
			Log.d(TAG, "BUMPRIGHT droid.getSpeed().getxDirection(): "
					+ droid.getSpeed().getxDirection() + "  ** droid.getX(): "
					+ droid.getX() + "  *** droid.getBitmap().getWidth() / 2: "
					+ droid.getBitmap().getWidth() / 2 + "  *** ");
		}
		// check collision with left wall if heading left
		if (droid.getSpeed().getxDirection() == Speed.DIRECTION_LEFT
				&& droid.getX() - droid.getBitmap().getWidth() / 2 <= 0) {
			droid.getSpeed().toggleXDirection();
			Log.d(TAG, "BUMPLEFT droid.getSpeed().getxDirection(): "
					+ droid.getSpeed().getxDirection() + "  ** droid.getX(): "
					+ droid.getX() + "  *** droid.getBitmap().getWidth() / 2: "
					+ droid.getBitmap().getWidth() / 2 + "  *** ");
		}
		// check collision with bottom wall if heading down
		if (droid.getSpeed().getyDirection() == Speed.DIRECTION_DOWN
				&& droid.getY() + droid.getBitmap().getHeight() / 2 >= getHeight()) {
			droid.getSpeed().toggleYDirection();
		}
		// check collision with top wall if heading up
		if (droid.getSpeed().getyDirection() == Speed.DIRECTION_UP
				&& droid.getY() - droid.getBitmap().getHeight() / 2 <= 0) {
			droid.getSpeed().toggleYDirection();
		}
		// Update the lone droid

		if (droid2.getSpeed().getxDirection() == Speed.DIRECTION_RIGHT
				&& droid2.getX() + droid2.getBitmap().getWidth() / 2 >= getWidth()) {
			droid2.getSpeed().toggleXDirection();
		}
		// check collision with left wall if heading left
		if (droid2.getSpeed().getxDirection() == Speed.DIRECTION_LEFT
				&& droid2.getX() - droid2.getBitmap().getWidth() / 2 <= 0) {
			droid2.getSpeed().toggleXDirection();
		}
		// check collision with bottom wall if heading down
		if (droid2.getSpeed().getyDirection() == Speed.DIRECTION_DOWN
				&& droid2.getY() + droid2.getBitmap().getHeight() / 2 >= getHeight()) {
			droid2.getSpeed().toggleYDirection();
		}
		// check collision with top wall if heading up
		if (droid2.getSpeed().getyDirection() == Speed.DIRECTION_UP
				&& droid2.getY() - droid2.getBitmap().getHeight() / 2 <= 0) {
			droid2.getSpeed().toggleYDirection();
		}
		/////////////
		if(Math.abs(droid.getX()-droid2.getX())<30&&Math.abs(droid.getY()-droid2.getY())<30){
			droid.getSpeed().toggleXDirection();
			droid2.getSpeed().toggleXDirection();
			droid.getSpeed().toggleYDirection();
			droid2.getSpeed().toggleYDirection();
		}
		

		// Update the lone droid
		droid.update();
		droid2.update();

	}

	public void render(Canvas canvas) {
		canvas.drawColor(Color.BLACK);
		droid.draw(canvas);
		droid2.draw(canvas);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// at this point the surface is created and
		// we can safely start the game loop
		thread.setRunning(true);
		thread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.d(TAG, "Surface is being destroyed");
		// tell the thread to shut down and wait for it to finish
		// this is a clean shutdown
		boolean retry = true;
		while (retry) {
			try {
				thread.join();
				retry = false;
			} catch (InterruptedException e) {
				// try again shutting down the thread
			}
		}
		Log.d(TAG, "Thread was shut down cleanly");
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			Log.d(TAG, "ACTION DOOOOWWWWWWWWNNNNN");
			// delegating event handling to the droid
			droid.handleActionDown((int) event.getX(), (int) event.getY());
			droid2.handleActionDown((int) event.getX(), (int) event.getY());

			// check if in the lower part of the screen we exit
			if (event.getY() > getHeight() - 50) {
				Log.d(TAG, "    overHEIGHT, STOPpING THREAD");
				thread.setRunning(false);
				((Activity) getContext()).finish();
			} else {
				Log.d(TAG,
						"    Coords: x=" + event.getX() + ",y=" + event.getY());
			}
		}
		if (event.getAction() == MotionEvent.ACTION_MOVE) {
			Log.d(TAG, "ACTION MOOOOOOOOOOVVVVVEEEE");
			// the gestures
			if (droid.isTouched()) {
				// the droid was picked up and is being dragged
				droid.setX((int) event.getX());
				droid.setY((int) event.getY());
			}
			if (droid2.isTouched()) {
				// the droid was picked up and is being dragged
				droid2.setX((int) event.getX());
				droid2.setY((int) event.getY());
			}
		}
		if (event.getAction() == MotionEvent.ACTION_UP) {
			Log.d(TAG, "ACTION UPPPPPpPPPPP");
			// touch was released
			if (droid.isTouched()) {
				droid.setTouched(false);
			}
			if (droid2.isTouched()) {
				droid2.setTouched(false);
			}
		}
		return true;
	}

}
